#include "Game.hh"
#include <iostream>
#include "../Utils/include/Common.hh"
#include "MainInterface.hh"
#include "Level.hh"
#include "../Utils/include/json.hpp"
#include <fstream>
using json = nlohmann::json;

using namespace std;

// 游戏类构造函数
Game::Game() : player_(nullptr), isRunning_(true), level_(nullptr) {}

// 游戏类析构函数
Game::~Game() {
    delete player_;
}

// 游戏主循环
void Game::run() {
    while (isRunning_) {
        switch(menu_.getChoice()) {
            case NEW_GAME:
                startNewGame();
                break;
            case LOAD_GAME:
                loadGame("Save.json");
                break;
            case HELP:
                menu_.showHelp();
                break;
            case EXIT:
                menu_.showExitMessage();
                isRunning_ = false;
                break;
            default:
                cout << "Invalid input, try again." << endl;
                Utility::pause();
        }
    }
}

// 引入动画所需头文件
#include <thread>
#include <chrono>

// 开始新游戏
void Game::startNewGame() {
    Utility::clearScreen();
    cout << "Start New Game" << endl;

    // 创建数据管理器并加载全部数据
    DataManager dm;
    dm.loadAllItems("database/Items.json");
    dm.loadAllEnemies("database/Enemy.json");
    dm.loadAllRooms("database/Room.json");

    // 播放开场剧情动画
    Utility::clearScreen();
    std::vector<std::string> introText = {
        "You are a CHU student"
        "One day you found an open manhole cover in the campus.",
        "When you look down, it seems like there is a magical dungeon beneath the campus",
        "Something is attracting you deep down...",
        "You try to enter this magical dungeon",
        "It's like you were reborn and gained a new identity",
        "You decide to find something deep inside that attracts you..."
    };

    for (const auto& line : introText) {
        std::cout << line << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    // 玩家角色选择
    auto players = DataManager::loadPlayers("database/Player.json");
    Player tempPlayer = DataManager::choosePlayer(players);
    player_ = new Player(tempPlayer);

    Utility::pause();

    // 初始化所有楼层地图数据
    vector<vector<vector<int>>> allFloors = {
        // 第一层地图
        {
            {1,  2,  3,  4,  0},
            {0,  5,  6,  7,  0},
            {0,  8,  9, 10,  0},
            {0, 11, 12, 13, 14},
            {0,  0,  0,  0, -1}
        },
        // 第二层地图
        {
            {101, 0, 102, 0, 103},
            {104,105,106,107, 0},
            {0,   0,108, 0,109},
            {110,111,112,113,114},
            {0,   0,  0,  0,  -2}
        },
        // 第三层地图
        {
            {201,202,203,204,205},
            {0,  0,206, 0,  0},
            {207,208,209,210,211},
            {0,  0,212, 0,  0},
            {213,214,215,216,-3}
        }
    };
    dm.loadAllFloors(allFloors);

    // 初始化游戏关卡
    Level gameLevel(dm);
    level_ = new Level(dm);
    level_->init(dm.getFloorMap(1));
    gameLevel.init(dm.getFloorMap(1));
    gameLevel.setCurrentFloor(1);
    
    // 处理楼层切换
    if (gameLevel.isFinished() && gameLevel.hasNextFloor()) {
        int nextFloor = gameLevel.getCurrentFloor() + 1;
        gameLevel.advanceToNextFloor(dm.getFloorMap(nextFloor));
    }

    Utility::pause();
    // 启动主界面
    MainInterface ui(*player_, gameLevel, dm,*this);
    ui.showLoop();

    Utility::pause();
}

// 退出游戏
void Game::exitGame() {
    isRunning_ = false;
}

// 加载游戏存档
void Game::loadGame(const std::string& path) {
    std::ifstream in(path);
    if (!in.is_open()) {
        std::cerr << "无法打开存档文件: " << path << std::endl;
        return;
    }

    // 读取存档数据
    json j;
    in >> j;
    in.close();

    // 重建玩家数据
    if (player_) delete player_;
    player_ = new Player(j.at("player").get<Player>());
    
    // 重建游戏世界数据
    DataManager dm;
    std::vector<std::vector<std::vector<int>>> allFloors = {
            {
                {1,  2,  3,  4,  0},
                {0,  5,  6,  7,  0},
                {0,  8,  9, 10,  0},
                {0, 11, 12, 13, 14},
                {0,  0,  0,  0, -1}
            },
            {
                {101, 0, 102, 0, 103},
                {104,105,106,107, 0},
                {0,   0,108, 0,109},
                {110,111,112,113,114},
                {0,   0,  0,  0,  -2}
            },
            {
                {201,202,203,204,205},
                {0,  0,206, 0,  0},
                {207,208,209,210,211},
                {0,  0,212, 0,  0},
                {213,214,215,216,-3}
            }
        };

    dm.loadAllFloors(allFloors);
    dm.loadAllItems("database/Items.json");
    dm.loadAllEnemies("database/Enemy.json");
    dm.loadAllRooms("database/Room.json");

    // 重建关卡数据
    level_ = new Level(dm);
    level_->fromJson(j.at("level"));
    std::cout << "Loading successfully: " << path << std::endl;
    Utility::pause();
    
    // 启动主界面
    MainInterface ui(*player_, *level_, dm, *this);
    ui.showLoop();
}